﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;
using V5_Model;

namespace V5_DAL
{
    public class DALProductOrderProduct
    {
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "K_ProductOrderProduct");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ProductOrderProduct");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelProductOrderProduct model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_ProductOrderProduct(");
            strSql.Append("ID,pid,prototalprice,promemberprice,promarketprice,proquantity,profieldvalues,proname,propic,proscore,OrderNum)");
            strSql.Append(" values (");
            strSql.Append("@ID,@pid,@prototalprice,@promemberprice,@promarketprice,@proquantity,@profieldvalues,@proname,@propic,@proscore,@OrderNum)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@pid", SqlDbType.Int,4),
					new SqlParameter("@prototalprice", SqlDbType.Int,4),
					new SqlParameter("@promemberprice", SqlDbType.Int,4),
					new SqlParameter("@promarketprice", SqlDbType.Int,4),
					new SqlParameter("@proquantity", SqlDbType.Int,4),
					new SqlParameter("@profieldvalues", SqlDbType.NVarChar,50),
					new SqlParameter("@proname", SqlDbType.NVarChar,200),
					new SqlParameter("@propic", SqlDbType.NVarChar,200),
					new SqlParameter("@proscore", SqlDbType.Int,4),
					new SqlParameter("@OrderNum", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.pid;
            parameters[2].Value = model.prototalprice;
            parameters[3].Value = model.promemberprice;
            parameters[4].Value = model.promarketprice;
            parameters[5].Value = model.proquantity;
            parameters[6].Value = model.profieldvalues;
            parameters[7].Value = model.proname;
            parameters[8].Value = model.propic;
            parameters[9].Value = model.proscore;
            parameters[10].Value = model.OrderNum;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelProductOrderProduct model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ProductOrderProduct set ");
            strSql.Append("pid=@pid,");
            strSql.Append("prototalprice=@prototalprice,");
            strSql.Append("promemberprice=@promemberprice,");
            strSql.Append("promarketprice=@promarketprice,");
            strSql.Append("proquantity=@proquantity,");
            strSql.Append("profieldvalues=@profieldvalues,");
            strSql.Append("proname=@proname,");
            strSql.Append("propic=@propic,");
            strSql.Append("proscore=@proscore,");
            strSql.Append("OrderNum=@OrderNum");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@pid", SqlDbType.Int,4),
					new SqlParameter("@prototalprice", SqlDbType.Int,4),
					new SqlParameter("@promemberprice", SqlDbType.Int,4),
					new SqlParameter("@promarketprice", SqlDbType.Int,4),
					new SqlParameter("@proquantity", SqlDbType.Int,4),
					new SqlParameter("@profieldvalues", SqlDbType.NVarChar,50),
					new SqlParameter("@proname", SqlDbType.NVarChar,200),
					new SqlParameter("@propic", SqlDbType.NVarChar,200),
					new SqlParameter("@proscore", SqlDbType.Int,4),
					new SqlParameter("@OrderNum", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.pid;
            parameters[2].Value = model.prototalprice;
            parameters[3].Value = model.promemberprice;
            parameters[4].Value = model.promarketprice;
            parameters[5].Value = model.proquantity;
            parameters[6].Value = model.profieldvalues;
            parameters[7].Value = model.proname;
            parameters[8].Value = model.propic;
            parameters[9].Value = model.proscore;
            parameters[10].Value = model.OrderNum;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductOrderProduct ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductOrderProduct GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,pid,prototalprice,promemberprice,promarketprice,proquantity,profieldvalues,proname,propic,proscore,OrderNum from K_ProductOrderProduct ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            ModelProductOrderProduct model = new ModelProductOrderProduct();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pid"].ToString() != "")
                {
                    model.pid = int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["prototalprice"].ToString() != "")
                {
                    model.prototalprice = int.Parse(ds.Tables[0].Rows[0]["prototalprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["promemberprice"].ToString() != "")
                {
                    model.promemberprice = int.Parse(ds.Tables[0].Rows[0]["promemberprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["promarketprice"].ToString() != "")
                {
                    model.promarketprice = int.Parse(ds.Tables[0].Rows[0]["promarketprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["proquantity"].ToString() != "")
                {
                    model.proquantity = int.Parse(ds.Tables[0].Rows[0]["proquantity"].ToString());
                }
                model.profieldvalues = ds.Tables[0].Rows[0]["profieldvalues"].ToString();
                model.proname = ds.Tables[0].Rows[0]["proname"].ToString();
                model.propic = ds.Tables[0].Rows[0]["propic"].ToString();
                if (ds.Tables[0].Rows[0]["proscore"].ToString() != "")
                {
                    model.proscore = int.Parse(ds.Tables[0].Rows[0]["proscore"].ToString());
                }
                model.OrderNum = ds.Tables[0].Rows[0]["OrderNum"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,pid,prototalprice,promemberprice,promarketprice,proquantity,profieldvalues,proname,propic,proscore,OrderNum ");
            strSql.Append(" FROM K_ProductOrderProduct ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,pid,prototalprice,promemberprice,promarketprice,proquantity,profieldvalues,proname,propic,proscore,OrderNum ");
            strSql.Append(" FROM K_ProductOrderProduct ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_ProductOrderProduct";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  成员方法
    }
}
